<?php
/*
RPHP Model
@category  Core
 */
RPHP::loadClass("ModelTrait", LIB_PATH);
class RPHP_Model
{
    use ModelTrait;
    //mysql
    protected $_dbconfig  = '_db';
    protected $_primarydb = 'primary';
    //redis
    protected $_redisconfig  = '_cache';
    protected $_primaryredis = 'primary';
    //
    protected $_conf           = null;
    protected $c               = null;
    protected $_dbcloaded      = false;
    protected $_redisconnected = false;
    public $_dbc               = array();
    public $_prefix            = array();
    public $_shop              = null;
    public $_user              = null;
    /**
     * Error
     */
    protected $_error;

    public function __construct($c)
    {
        $this->c                          = $c;
        $this->_conf                      = RPHP::loadConfig("config/db");
        $this->_prefix[$this->_primarydb] = $this->_conf[$this->_dbconfig][$this->_primarydb]['prefix'];

    }

    public function __destruct()
    {
        if ($this->_dbc[$this->_primarydb]) {
            $this->db->close();
        }

        if ($this->_redisconnected) {
            $this->redis->close();
        }

    }

    public function redis()
    {
        if (!$this->_redisconnected) {
            $this->_redisconnected = true;
            RPHP::loadClass($this->_conf[$this->_redisconfig]['adapter'], COM_PATH . DIRECTORY_SEPARATOR . "Db");
            $class = "RPHP_Db_" . $this->_conf[$this->_redisconfig]['adapter'];
            return new $class($this->_conf[$this->_redisconfig][$this->_primaryredis]);
        }

    }

    public function db($server = null)
    {
        if (!$this->_dbcloaded) {
            $this->loadDbc();
            $this->_dbcloaded = true;
        }
        if (!$server) {
            $server = $this->_primarydb;
        }

        $class                  = "RPHP_Db_" . $this->_conf[$this->_dbconfig]['adapter'];
        $this->_dbc[$server]    = 1;
        $this->_prefix[$server] = $this->_conf[$this->_dbconfig][$server]['prefix'];
        return new $class($this->_conf[$this->_dbconfig][$server]);

    }

    public function loadDbc()
    {
        RPHP::loadClass("Abstract", COM_PATH . DIRECTORY_SEPARATOR . "Db");
        RPHP::loadClass($this->_conf[$this->_dbconfig]['adapter'], COM_PATH . DIRECTORY_SEPARATOR . "Db");
    }

    public function sql($sql)
    {
        $result = $this->db->sql($sql);
        return $result;
    }

/*-extend----------------------------*/

    //auth
    public function auth()
    {
        RPHP::loadClass("auth", "extend");
        return new RPHP_Auths($this->db, $this->c, $this->_prefix[$this->_primarydb]);
    }

    //function class
    public function fun()
    {
        RPHP::loadClass("Function", LIB_PATH);
        return new RPHP_Functions();
    }
    //upload class
    public function upload()
    {
        RPHP::loadClass("Upload", LIB_PATH);
        return new RPHP_Upload();
    }
    //Pinyin
    public function pinyin(){
        RPHP::loadClass("Pinyin",LIB_PATH);
        return new Pinyin();
    }

/*-magic-----------*/

    public function __set($key, $value = null)
    {
        $this->$key = $value;
    }

    public function __get($key)
    {
        switch ($key) {
            case 'db':
                $this->db = $this->db();
                return $this->db;
            case 'redis':
                $this->redis = $this->redis();
                return $this->redis;
            case 'auth':
                $this->auth = $this->auth();
                return $this->auth;
            case 'fun':
                $this->fun = $this->fun();
                return $this->fun;
            case 'upload':
                $this->upload = $this->upload();
                return $this->upload;
            case 'pinyin':
                $this->pinyin = $this->pinyin();
                return $this->pinyin;
        }
    }

}
